<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox" v-model="isAllCheck"/> <!-- 该checkbox具有get和set的特性 -->
    </label>
    <span>
          <span>已完成{{completeSize}}</span> / 全部{{todos.length}}
        </span>
<!--    <button class="btn btn-danger" @click="deleteAllCompletedTodos">清除已完成任务</button> &lt;!&ndash; 直接调用deleteAllCompletedTodos &ndash;&gt;-->
    <slot name="completed"/>
  </div>
</template>

<script>
  export default {
    name: 'TodoFooter',
    props: {
      todos: Array,
      deleteAllCompletedTodos: Function,// 删除所有已完成的任务
      selectAllTodos: Function// 操作全选 checkbox
    },
    computed: {
      completeSize () {// 以为todos发生变化时，completeSize要重新计算
        return this.todos.reduce((preTotal, todo) => preTotal + (todo.complete?1:0), 0)// 初始值为0，完成的数目+1使用reduce统计
      },
      isAllCheck: {
        get () {// 读取
          return this.completeSize===this.todos.length && this.todos.length > 0// completeSize可以直接调用
        },
        set (check) {// 设置check选项，调用父类传入的方法实现
          this.selectAllTodos(check)
        }
      }
    },
    methods: {

    }
  }
</script>

<style scoped>
  /*footer*/
  .todo-footer {
    height: 40px;
    line-height: 40px;
    padding-left: 6px;
    margin-top: 5px;
  }

  .todo-footer label {
    display: inline-block;
    margin-right: 20px;
    cursor: pointer;
  }

  .todo-footer label input {
    position: relative;
    top: -1px;
    vertical-align: middle;
    margin-right: 5px;
  }

  .todo-footer button {
    float: right;
    margin-top: 5px;
  }

</style>
